using System;
using System.Collections.Generic;
using System.Text;

using TTDotNet.Util;
using TTDotNet.Engine;
using TTDotNet.Engine.Terrain;
using TTDotNet.Engine.ThreeDEngine;
using System.Drawing;
using TTDotNet.Engine.Networks;

namespace TTDotNet.Engine.Create.Tools
{
    public class BuildTunnelTool : Tool
    {
        //private DDDCoordinate dragStart;
        //private RotateType dragStartSide;

        //private Coordinate highlighted = Coordinate.Invalid;

        private List<Coordinate> hightlightedSides = new List<Coordinate>();
        private int tunnelHeight = -1;

        public BuildTunnelTool(Game game) : base(game, ToolType.RoadTunnel)
        {
            //dragStart = new DDDCoordinate(-1, -1, -1);
            //dragStartSide = RotateType.Invalid;
        }

        public override void OnDeactivated()
        {
            //if(!highlighted.IsInvalid)
            //    Game.gameWorld.Map.UpdateHighlight(highlighted.X, highlighted.Z, HighlightType.NoHighlight, RotateType.Zero);
        }

        public override void MouseMove(Point mouseLocation, GridPickData data)
        {
            foreach (Coordinate coord2 in hightlightedSides)
            {
                Game.gameWorld.Map.UpdateHighlight(coord2.X, coord2.Z, HighlightType.NoHighlight, RotateType.Zero);
            }
            hightlightedSides.Clear();

            Coordinate coord = data.Coord;

            int incX = 0;
            int incZ = 0;

            if (!coord.IsInvalid)
            {
                SlopeType slope = Game.gameWorld.Map.GetTileSlopeType(coord.X, coord.Z);

                if (slope == SlopeType.FlatSlope)
                {
                    

                    int ne = Game.gameWorld.Map.GetCornerHeight(coord.X, coord.Z, Direction.NE);
                    int se = Game.gameWorld.Map.GetCornerHeight(coord.X, coord.Z, Direction.SE);
                    int sw = Game.gameWorld.Map.GetCornerHeight(coord.X, coord.Z, Direction.SW);
                    int nw = Game.gameWorld.Map.GetCornerHeight(coord.X, coord.Z, Direction.NW);

                    if (ne == se && ne > sw)
                        incX = 1;
                    else if (se == sw && se > nw)
                        incZ = -1;
                    else if (sw == nw && sw > ne)
                        incX = -1;
                    else if (nw == ne && nw > sw)
                        incZ = 1;

                    int tunnelTop = Math.Max(Math.Max(ne, se), Math.Max(sw, nw));
                    bool validTunnel = false;
                    Coordinate nextCoord = data.Coord;
                    hightlightedSides.Add(nextCoord);
                    nextCoord.X += incX;
                    nextCoord.Z += incZ;

                    while (!nextCoord.IsInvalid)
                    {
                        hightlightedSides.Add(nextCoord);

                        int minOfTile = Game.gameWorld.Map.GetTileMinHeight(nextCoord.X, nextCoord.Z);

                        if (minOfTile < tunnelTop)
                        {
                            SlopeType otherEnd = Game.gameWorld.Map.GetTileSlopeType(nextCoord.X, nextCoord.Z);

                            if (otherEnd == SlopeType.FlatSlope)
                            {
                                ////WOOO
                                //int incX2, incZ2;

                                //int ne2 = Game.gameWorld.Map.GetCornerHeight(nextCoord.X, nextCoord.Z, Direction.NE);
                                //int se2 = Game.gameWorld.Map.GetCornerHeight(nextCoord.X, nextCoord.Z, Direction.SE);
                                //int sw2 = Game.gameWorld.Map.GetCornerHeight(nextCoord.X, nextCoord.Z, Direction.SW);
                                //int nw2 = Game.gameWorld.Map.GetCornerHeight(nextCoord.X, nextCoord.Z, Direction.NW);

                                //if (ne2 == se2 && ne2 > sw2)
                                //    incX2 = 1;
                                //else if (se2 == sw2 && se2 > nw2)
                                //    incZ2 = -1;
                                //else if (sw2 == nw2 && sw2 > ne2)
                                //    incX2 = -1;
                                //else if (nw2 == ne2 && nw2 > sw2)
                                //    incZ2 = 1;

                                //if (incX * -1 == incX2 && incZ * -1 == incZ2)
                                //{
                                //    validTunnel = true;
                                //}
                                //else
                                //{
                                //    break;
                                //}
                                validTunnel = true;
                                break;
                            }
                            else
                            {
                                break;
                            }
                        }

                        nextCoord.X += incX;
                        nextCoord.Z += incZ;
                    }

                    if (validTunnel)
                    {
                        foreach (Coordinate hCoord in hightlightedSides)
                        {
                            Game.gameWorld.Map.UpdateHighlight(hCoord.X, hCoord.Z, HighlightType.Full, RotateType.Zero);
                        }
                        tunnelHeight = tunnelTop - 2;
                    }
                    else
                    {
                        hightlightedSides.Clear();
                    }

                }

               // Console.WriteLine("It is: " + slope.ToString() + " and: " + incX + "|" + incZ);
            }

            //try
            //{
            //    if (!dragStart.IsInvalid && !data.Coord.IsInvalid)
            //    {
            //        RoadStrip strip = GetRoadStrip(dragStart, dragStartSide,
            //                            new DDDCoordinate(data.Coord.X, Game.gameWorld.Map.GetTileHeight(data.Coord), data.Coord.Z), data.TileSide);

            //        if (strip.coords.Count > 4)
            //        {

            //        }

            //        List<CoordSide> coordSides = new List<CoordSide>();
            //        RotateType type = strip.start;

            //        DDDCoordinate last = new DDDCoordinate(-1, -1, -1);

            //        foreach (DDDCoordinate coord in strip.coords)
            //        {
            //            if (last.Equals(coord))
            //                Game.gameWorld.Map.UpdateHighlight(coord.X, coord.Z, HighlightType.Full, RotateType.Zero);
            //            else
            //                Game.gameWorld.Map.UpdateHighlight(coord.X, coord.Z, HighlightType.Half, type);

            //            coordSides.Add(new CoordSide(coord, type));
            //            type = (RotateType)(((int)type + 2) % 4);

            //            last = coord;
            //        }

            //        hightlightedSides = coordSides;
                    
            //    }

            //}
            //catch (Exception e)
            //{

            //}

        }

        public override void MouseDown(Point mouseLocation, GridPickData data)
        {
            if (hightlightedSides.Count >= 2)
            {
                int count = hightlightedSides.Count;

                Game.gameWorld.Map.Grid.ToggleTileOff(hightlightedSides[0].X, hightlightedSides[0].Z);

                Game.gameWorld.Map.Grid.ToggleTileOff(hightlightedSides[count - 1].X, hightlightedSides[count - 1].Z);

                    Game.gameWorld.RoadNetwork.AddTunnel(new DDDCoordinate(hightlightedSides[0].X, tunnelHeight, hightlightedSides[0].Z),
                                                        new DDDCoordinate(hightlightedSides[count - 1].X, tunnelHeight, hightlightedSides[count - 1].Z));
                
            }
        }

    }

    
}
